package com.sam.widget.text_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

import com.sam.widget.R;

/**
 * Created by Administrator on 2016/6/6.
 */
public class CustomFontTextView extends TextView {
    private Context context;
    private String fontPath;

    public CustomFontTextView(Context context) {
        this(context,null);
    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        getAttrs(attrs);
        setCustomFontInAssets(fontPath);
    }

    private void getAttrs(AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);
        fontPath = ta.getString(R.styleable.CustomFontTextView_fontPathInAssets);
        ta.recycle();
    }


    public void setCustomFontInAssets(String fontPath){
        try {
            if(!TextUtils.isEmpty(fontPath)) setTypeface(Typeface.createFromAsset(context.getAssets(),fontPath));
        } catch (Exception e){
            Log.e("Sam","你配置的字体路径有误，在Assets目录下找不到字体:"+fontPath);
            e.printStackTrace();
        }
    }
}
